package com.example.carbon.controller;

import com.example.carbon.pojo.dto.EnergySearch;
import com.example.carbon.pojo.dto.PageLimit;
import com.example.carbon.pojo.vo.RestBean;
import com.example.carbon.server.info.EnergyServer;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * 用于环境信息采集表得controller层
 */
@RestController
@RequestMapping("api/energy")
@Slf4j
public class EnergyDataController {
    @Resource
    EnergyServer energyServer;

    /**
     * 通过restful风格来进行数据的读取，然后主要传参有page,item,order
     * page用于页数，默认为0
     * item用于每页个数 默认为10
     * order为排序字段，默认id降序
     *
     * @param page
     * @param item
     * @return
     */
    @RequestMapping("/info/{page}/{item}")
    public RestBean searChTableData(@PathVariable(value = "item") int item, @PathVariable("page") int page,
                                    @RequestBody EnergySearch search) {
        try {
            PageLimit pageLimit = new PageLimit(page, item);
            return RestBean.success(energyServer.getEnergyInfo(pageLimit, search));
        } catch (Exception e) {
            log.info(e.getMessage());
            return RestBean.failure("获取数据失败");
        }
    }
}
